package com.yanqu.road.dao.impl.recharge;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.recharge.GoldTokenOrder;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PlatformGoldTokenOrderDaoImpl extends TempDao {

	public List<GoldTokenOrder> getGoldTokenOrderList(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		List<GoldTokenOrder> dataList = new ArrayList<>();
		String sql = "select * from t_u_platform_gold_token_order where userId =?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				dataList.add(parseGoldTokenOrder(rs));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public GoldTokenOrder getGoldTokenOrder(String orderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_platform_gold_token_order where orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, orderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				GoldTokenOrder data = parseGoldTokenOrder(rs);
				return data;
			}
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public boolean addGoldTokenOrder(String orderNo, long userId, long money, long time) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_platform_gold_token_order(orderNo,orderStatus,userId,money,time,createTime,updateTime) " +
				"VALUES (?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, orderNo);
			call.setInt(i++, 0);
			call.setLong(i++, userId);
			call.setLong(i++, money);
			call.setLong(i++, time);
			long nowTime = System.currentTimeMillis();
			call.setTimestamp(i++, new Timestamp(nowTime));
			call.setTimestamp(i++, new Timestamp(nowTime));
            return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean checkGoldTokenOrder(String orderNo) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_platform_gold_token_order where orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setString(1, orderNo);
			rs = call.executeQuery();
			while (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return false;
	}

	public boolean updateOrderStatus(GoldTokenOrder goldTokenOrder) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_platform_gold_token_order SET orderStatus=?,updateTime=? WHERE orderNo=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, goldTokenOrder.getOrderStatus());
			call.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
			call.setString(i++, goldTokenOrder.getOrderNo());
			return call.executeUpdate() > 0;
		} catch (SQLException e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	private GoldTokenOrder parseGoldTokenOrder(ResultSet rs) throws SQLException {
		GoldTokenOrder data = new GoldTokenOrder();
		data.setOrderNo(rs.getString("orderNo"));
		data.setOrderStatus(rs.getInt("orderStatus"));
		data.setUserId(rs.getLong("userId"));
		data.setMoney(rs.getLong("money"));
		data.setCreateTime(new Date(rs.getTimestamp("createTime").getTime()));
		data.setUpdateTime(new Date(rs.getTimestamp("updateTime").getTime()));
		return data;
	}
}
